﻿#region << 文件说明 >>

/*----------------------------------------------------------------
 * 命名空间：Vampirewal.BlogCore.Controls
 * 唯一标识：4426c5b2-49af-4d62-b2b4-acc78bb2c640
 * 文件名：Code
 *
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2024/6/25 10:59:25
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/

#endregion << 文件说明 >>

namespace Vampirewal.BlogCore;

/// <summary>
/// 代码高亮控件
/// </summary>
public sealed class Code : ControlBase
{
    /// <summary>
    /// 构造函数
    /// </summary>
    internal Code()
    {
    }

    public override string Id { get; set; }
    public override string Title { get; set; }

    /// <summary>
    /// 语言
    /// </summary>
    internal string Language { get; set; }

    public override string ConverToHtml()
    {
        StringBuilder sb = new StringBuilder();

        sb.AppendLine($"<pre class=\"theme-atom-one-dark shadow-3xl text-sm relative overflow-hidden max-w-full tab-size h-full\">");
        sb.AppendLine($"    <span class=\"hljs mb-0 p-4 block min-h-full overflow-auto\">");
        sb.AppendLine($"        <code class=\"{Language}\">");

        sb.AppendLine($"            {Title}");

        sb.AppendLine($"        </code>");
        sb.AppendLine($"    </span>");
        sb.AppendLine($"</pre>");

        return sb.ToString();
    }
}

public static partial class App
{
    /// <summary>
    /// 创建代码块
    /// </summary>
    /// <param name="content">内容</param>
    /// <param name="language">语言</param>
    /// <returns></returns>
    public static Code CreateCode(string content, string language = "csharp")
    {
        Code code = new Code
        {
            Title = content,
            Language = language
        };

        return code;
    }

    /// <summary>
    /// 创建代码块
    /// </summary>
    /// <param name="content">内容</param>
    /// <param name="language">语言</param>
    /// <returns></returns>
    public static Code CreateCode(Func<string> content, string language = "csharp")
    {
        Code code = new Code
        {
            Language = language
        };

        code.Title = content.Invoke();

        return code;
    }
}